//
//  AppDelegate.m
//  YanRenjie
//
//  Created by dllo on 15/10/23.
//  Copyright (c) 2015年 yanrenjie. All rights reserved.
//

#import "AppDelegate.h"
#import "YRJRecommendViewController.h"
#import "YRJDestinationViewController.h"
#import "YRJMineViewController.h"
#import "YRJAdviceViewController.h"
//#import <ShareSDK/ShareSDK.h>
#import "YRJShowCenterViewController.h"
#import "UMSocial.h"
#import "YRJDetectionNetworkStatusTool.h"
//#import <SMS_SDK/SMSSDK.h>
#import "YRJSingleton.h"

#define appKey @"5b2655c71290"
#define appSecret @"55988074b9a3faadffa6f74cd3ae7845"

#define WIDTH ([UIScreen mainScreen].bounds.size.width / 375)
#define HEIGH ([UIScreen mainScreen].bounds.size.height / 667)
@interface AppDelegate ()<UIScrollViewDelegate>
@property (nonatomic, retain) UIPageControl *page;
@end

@implementation AppDelegate
- (void)dealloc {
    [_window release];
    
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];
    
    //初始化应用，appKey和appSecret从后台申请得到
//    [SMSSDK registerApp:appKey
//             withSecret:appSecret];
    [UMSocialData setAppKey:@"563ab1eee0f55a83c5005570"];
    
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"everLaunched"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        [self createTabBar];
        
        UIScrollView *myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
        myScroll.contentSize = CGSizeMake(self.window.frame.size.width * 3, self.window.frame.size.height);
        for (int i = 0; i < 3; i++) {
            UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.window.frame.size.width * i, 0, self.window.frame.size.width, self.window.frame.size.height)];
            
            myImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"yindaoye%d.png", i]];
            [myScroll addSubview:myImage];
        }
        
        myScroll.pagingEnabled = YES;
        myScroll.showsHorizontalScrollIndicator = NO;
        myScroll.bounces = NO;
        myScroll.delegate = self;
        [self.window addSubview:myScroll];
        
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [myScroll addSubview:button];
        button.frame = CGRectMake(self.window.frame.size.width * 2 + self.window.frame.size.width / 2 - 80 * WIDTH, self.window.frame.size.height - 80 * HEIGH, 150, 40);
        [button setTitle:@"欢迎使用游缘不止" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(myAction) forControlEvents:(UIControlEventTouchUpInside)];

        self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 620 * HEIGH, self.window.frame.size.width, 30)];
        self.page.numberOfPages = 3;
        self.page.currentPage = 0;
        self.page.backgroundColor = [UIColor clearColor];
        self.page.currentPageIndicatorTintColor = [UIColor cyanColor];
        self.page.pageIndicatorTintColor = [UIColor whiteColor];
        [self.window addSubview:self.page];
        
    } else {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
        [self createTabBar];
    }
    
    // 进入程序的时候设置引用的白天还是夜间的模式
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"isNight.txt"];
    NSString *string = [NSString stringWithContentsOfFile:docPath encoding:NSUTF8StringEncoding error:nil];
    if ([string isEqualToString:@"0"]) {
        self.window.alpha = 0.2;
    } else {
        self.window.alpha = 1;
    }
    
    // 进入程序的时候设置用户的登录状态为未登录
    [YRJSingleton shareSingleton].isLogin = NO;

    // 检测网络
     [YRJDetectionNetworkStatusTool sharedClient];
    
    
    return YES;
}

- (void)createTabBar {
    // 推荐
    YRJRecommendViewController *recommentVC = [[YRJRecommendViewController alloc] init];
    UINavigationController *recommentNaVC = [[UINavigationController alloc] initWithRootViewController:recommentVC];
    recommentNaVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"推荐" image:[UIImage imageNamed:@"tuijian.png"] tag:1001] autorelease];
    
    // 晒图中心
    YRJShowCenterViewController *showCenterVC = [[YRJShowCenterViewController alloc] init];
    UINavigationController *showCenterNaVC = [[UINavigationController alloc] initWithRootViewController:showCenterVC];
    showCenterNaVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"秀一秀" image:[UIImage imageNamed:@"xiu.png"] tag:1007] autorelease];
    
    // 目的地
    YRJDestinationViewController *destinationVC = [[YRJDestinationViewController alloc] init];
    UINavigationController *destinationNaVC = [[UINavigationController alloc] initWithRootViewController:destinationVC];
    destinationNaVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"目的地" image:[UIImage imageNamed:@"mudedi.png"] tag:1004] autorelease];
    
    // 个人中心
    YRJMineViewController *mineVC = [[YRJMineViewController alloc] init];
    UINavigationController *mineNaVC = [[UINavigationController alloc] initWithRootViewController:mineVC];
    mineNaVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"个人中心" image:[UIImage imageNamed:@"gerenzhongxin.png"] tag:1005] autorelease];
    
    // 锦囊妙计
    YRJAdviceViewController *adviceVC = [[YRJAdviceViewController alloc] init];
    UINavigationController *adviceNaVC = [[UINavigationController alloc] initWithRootViewController:adviceVC];
    adviceNaVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"锦囊" image:[UIImage imageNamed:@"jinnang.png"] tag:1006] autorelease];
    
    UITabBarController *tab = [[UITabBarController alloc] init];
    tab.viewControllers = @[recommentNaVC, destinationNaVC, showCenterNaVC, mineNaVC];
    tab.tabBar.translucent = NO;
    tab.tabBar.barTintColor = [UIColor colorWithRed:70 / 255.0 green:190 / 255.0 blue:200 / 255.0 alpha:1];
    // 选中后的颜色
    tab.tabBar.tintColor = [UIColor brownColor];
    tab.selectedIndex = 0;
    self.window.rootViewController = tab;
    [tab release];
    
    [recommentNaVC release];
    [recommentVC release];
    [destinationNaVC release];
    [destinationVC release];
    [showCenterNaVC release];
    [showCenterVC release];
    [mineNaVC release];
    [mineVC release];
    [adviceNaVC release];
    [adviceVC release];
}


- (void)myAction {
    [self createTabBar];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.page.currentPage = scrollView.contentOffset.x / scrollView.window.frame.size.width;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
